﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Services.DataCache;
using Enter.Services.Mail;
using Enter.Common.validcode;
using System.Drawing;
using Enter.Mvc.Base.Controllers;
using System.Web.Mvc;
using Enter.Mvc.Mall.Model;
using System.Web;
using Enter.Common;
using System.Dynamic;
using Enter.Config;
using System.Reflection;
using Enter.Entity;
using System.Drawing.Imaging;
using System.Collections;
using Enter.Mvc.Mall.Model.User;
using Newtonsoft.Json;

namespace Enter.Mvc.Mall.Controllers
{
    public class CommonController : BaseControllers
    {
        private readonly ICache icache;
        private readonly IMail imail;

        public CommonController(ICache cache, IMail mail)
        {
            this.icache = cache;
            this.imail = mail;
        }
        #region 验证码
        public ActionResult GetValidateCode(int length)
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(length);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }
        public ActionResult CheckValidateCode(string Code)
        {

            var results = new JsonResultModel();
            if (this.HttpContext.Session["ValidateCode"] != null && Session["ValidateCode"].ToString() == Code)
            {
                results.ResultCode = "00";
                results.ResultMsg = "验证码输入正确";
            }
            else
            {
                results.ResultCode = "01";
                results.ResultMsg = "验证码输入错误";
            }
            return Json(results);
        }
        #endregion

        #region 上传图片
        public UploadModel uploadImg(Enter.Config.ImgRootType imgType, string imgID, string hfImgUrl, string msg)
        {
            UploadModel Model = new UploadModel();
            Model.imgType = (int)imgType;
            Model.imgID=imgID;
            Model.hfImgUrl = hfImgUrl;
            Model.msg = msg;
            return Model;
        }

       

        [HttpPost]
        public ActionResult Upload()
        {
             string returnStr = "";
            try
            {


                string imgtype = HttpContext.Request["imgtype"];
                HttpPostedFileBase FUpload = HttpContext.Request.Files[0] as HttpPostedFileBase;

                string fileName = "";
                string UploadPath, ServerPath;
                int width = 0, height = 0, size = 0;
               
                imail.GetUploadPath((ImgRootType)byte.Parse(imgtype), out UploadPath, out ServerPath, out  width, out  height, out  size);
                if (FUpload.ContentLength > 0 && FUpload.ContentLength < size * 1204)
                {
                    UploadUnit Upload = new UploadUnit(UploadPath);
                    fileName = Upload.UpLoadImg(FUpload, Convert.ToInt32(imgtype));
                    if (width != 0)
                    {
                        string imgFullPath="";
                        if (ImgHelper.CreateImg(UploadPath + fileName, UploadPath, Convert.ToInt32(imgtype).ToString(), width, height, out imgFullPath))
                        {
                            returnStr = "00|" + ServerPath + imgFullPath;
                            FileHelper.Delete(fileName);
                        }
                        else {
                            FileHelper.Delete(fileName);
                            returnStr = "01|上传失败，请重新上传";
                        }
                    }
                    else
                    {
                        returnStr = "00|" + ServerPath +"-"+ fileName;
                    }

                }
                else
                {
                    returnStr = string.Format("01|文件过大，请上传{0}K以内的图片", size);
                }
            }
            catch (Exception ex)
            {

                returnStr = "01|" + ex.Message;
            }
            return Content(returnStr);

        }
      
        #endregion
        #region 网站区域
        [HttpPost]
        public ActionResult GetArea(string AreaNo)
        {
            
          List<T9002_AreaEntity> list=   imail.GetAreaList();

          List<T9002_AreaEntity> flist = null;
          if (AreaNo == "0")
          {
              flist = list.FindAll(area => area.AreaNO == UserInfo.CurrentUser.AreaNo);
          }
          else
          {
              flist = list.FindAll(area => area.ParentNo == UserInfo.CurrentUser.AreaNo);
          }
          List<AreaModel> areaList = new List<AreaModel>();
          if (flist != null)
          {
              foreach (T9002_AreaEntity entity in flist)
              {
                  AreaModel model = new AreaModel();
                  model.AreaNO = entity.AreaNO;
                  model.AreaName = entity.AreaName;
                  areaList.Add(model);
              }
          }
          object obj = new { list = areaList };
          
           string result = JavaScriptConvert.SerializeObject(obj);
           return Content(result);//Json(obj);
        }
      
        #endregion

        #region 首页
        public ActionResult topScreen()
        {

            return View();
        }
        static int i = 1;

    protected string innerid;

    

        public ActionResult floorLoading()
        {
    innerid = "floor_" + i++;
            ViewData["innerid"] = innerid;
            return View();
        }
        public ActionResult layoutFPoster(string innerid)
        {
            ViewData["innerid"] = innerid;
            return PartialView();

        }
        #endregion
    }
      
}
